﻿using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Collections.Generic;

namespace WebUpdaterAdminCore.Data.Migrations
{
    public partial class InitialDB : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "SocketSession");

            migrationBuilder.CreateTable(
                name: "ClientGroups",
                columns: table => new
                {
                    Id = table.Column<int>(nullable: false)
                        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                    Name = table.Column<string>(maxLength: 100, nullable: false),
                    Remakrs = table.Column<string>(maxLength: 500, nullable: true),
                    UpdateDate = table.Column<DateTime>(nullable: false),
                    Usable = table.Column<bool>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ClientGroups", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "ClientStatus",
                columns: table => new
                {
                    SessionId = table.Column<string>(nullable: false),
                    ClientIP = table.Column<string>(nullable: true),
                    Connected = table.Column<bool>(nullable: false),
                    LastActiveTime = table.Column<DateTime>(nullable: false),
                    StartTime = table.Column<DateTime>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ClientStatus", x => x.SessionId);
                });

            migrationBuilder.CreateTable(
                name: "ClientInfos",
                columns: table => new
                {
                    ClientId = table.Column<string>(nullable: false),
                    ClientName = table.Column<string>(nullable: true),
                    GroupId = table.Column<int>(nullable: true),
                    Remarks = table.Column<string>(nullable: true),
                    SessionId = table.Column<string>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ClientInfos", x => x.ClientId);
                    table.ForeignKey(
                        name: "FK_ClientInfos_ClientGroups_GroupId",
                        column: x => x.GroupId,
                        principalTable: "ClientGroups",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_ClientInfos_ClientStatus_SessionId",
                        column: x => x.SessionId,
                        principalTable: "ClientStatus",
                        principalColumn: "SessionId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_ClientInfos_GroupId",
                table: "ClientInfos",
                column: "GroupId");

            migrationBuilder.CreateIndex(
                name: "IX_ClientInfos_SessionId",
                table: "ClientInfos",
                column: "SessionId",
                unique: true,
                filter: "[SessionId] IS NOT NULL");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "ClientInfos");

            migrationBuilder.DropTable(
                name: "ClientGroups");

            migrationBuilder.DropTable(
                name: "ClientStatus");

            migrationBuilder.CreateTable(
                name: "SocketSession",
                columns: table => new
                {
                    ClientId = table.Column<string>(nullable: false),
                    ClientName = table.Column<string>(nullable: true),
                    Connected = table.Column<bool>(nullable: false),
                    GroupId = table.Column<int>(nullable: false),
                    LastActiveTime = table.Column<DateTime>(nullable: false),
                    Remarks = table.Column<string>(nullable: true),
                    RemoteIP = table.Column<string>(nullable: true),
                    SessionId = table.Column<string>(nullable: true),
                    StartTime = table.Column<DateTime>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_SocketSession", x => x.ClientId);
                });
        }
    }
}
